package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.SpecialOffer;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.service.SpecialOfferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/04 10:13
 * 
 * 特价活动和秒杀活动的管理类
 */
@RestController
@RequestMapping("/specialOffer/")
public class SpecialOfferController {
    
    @Autowired
    SpecialOfferService specialOfferService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     */
    @GetMapping("/list/{page}/{size}/{draw}/{type}")
    public Response list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size,
                         @PathVariable("draw") String draw,
                         @PathVariable("type") Integer type){
        return specialOfferService.selectAllByPage(page, size, draw, type);
    }

    /**
     *  APP  首页 获取 举办特价会场详情
     *
     *  '活动的类型---1特价活动---2秒杀活动',   sql 默认是1
     */
    @GetMapping("/getSpecialOffer")
    public Response getSpecialOffer(){
        return specialOfferService.getSpecialOffer();
    }
    
    /**
     * 添加会场活动
     * @param request
     * @param specialOffer
     * @param img
     * @return
     */
    @PostMapping("/save")
    public Response save(HttpServletRequest request, SpecialOffer specialOffer, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                specialOffer.setSpecialOfferImg(result);
                response = specialOfferService.save(specialOffer);
            }
        }else{
            response = specialOfferService.save(specialOffer);
        }
        return response;
    }

    /**
     * 修改
     * @param request
     * @param specialOffer
     * @param img
     * @return
     */
    @PostMapping("/update")
    public Response update(HttpServletRequest request, SpecialOffer specialOffer, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                specialOffer.setSpecialOfferImg(result);
                response = specialOfferService.update(specialOffer);
            }
        }else{
            response = specialOfferService.update(specialOffer);
        }
        return response;
    }

    /**
     * 查询会场详情
     * @param specialOfferId:会场的id
     * @return
     */
    @GetMapping("/getBrandById/{id}")
    public Response getBrandById(@PathVariable("id")  String specialOfferId){
        return specialOfferService.getSpecialOfferById(specialOfferId);
    }

    /**
     * 删除
     * @param specialOfferId:会场的id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id")  String specialOfferId){
        return specialOfferService.deleteById(specialOfferId);
    }

    /**
     * 更改会场的状态
     * @param specialOfferId:会场的id
     * @return
     */
    @GetMapping("/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id")  String specialOfferId,
                                            @PathVariable("status")  Integer status){
        return specialOfferService.changeStatus(specialOfferId,status);
    }

    /**
     * 批量删除
     * @param id:活动的id集合
     * @return
     */
    @DeleteMapping("/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return specialOfferService.batchDeleteById(id);
    }
}
